package com.piepesken.mediscale.ui.drugs

import androidx.compose.foundation.layout.*
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.LargeTopAppBar
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.piepesken.mediscale.models.Drug
import com.piepesken.mediscale.viewmodels.DrugViewModel
import com.piepesken.mediscale.R

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DrugDetails(
    navController: NavHostController,
    drugId: Int?
) {
    val viewModel: DrugViewModel = hiltViewModel()

    if (drugId == null) {
        return
    }

    val drug: Drug by viewModel.getDrugById(drugId).collectAsState(initial = Drug(
        -1, "", "", "", ""
    ))

    if (drug.id == -1) {
        Column (
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(text = stringResource(id = R.string.itemNotExists))
        }
    }

    Column(
        modifier = Modifier.fillMaxSize()
    ) {
        LargeTopAppBar(
            title = {
                Text(
                    drug.name,
                    fontSize = MaterialTheme.typography.displaySmall.fontSize,
                    fontStyle = MaterialTheme.typography.displaySmall.fontStyle,
                    fontWeight = MaterialTheme.typography.displaySmall.fontWeight
                )
            }
        )

        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp)
        ) {
            drug.ingredients?.let {
                if (!"".equals(it)) {
                    Text(
                        text = stringResource(id = R.string.drugIngredients),
                        lineHeight = MaterialTheme.typography.titleLarge.lineHeight,
                        fontSize = MaterialTheme.typography.titleLarge.fontSize,
                        fontWeight = MaterialTheme.typography.titleLarge.fontWeight,
                        fontStyle = MaterialTheme.typography.titleLarge.fontStyle
                    )

                    Text(
                        text = it,
                        lineHeight = MaterialTheme.typography.bodyMedium.lineHeight,
                        fontSize = MaterialTheme.typography.bodyMedium.fontSize,
                        fontStyle = MaterialTheme.typography.bodyMedium.fontStyle,
                        fontWeight = MaterialTheme.typography.bodyMedium.fontWeight
                    )

                    Spacer(modifier = Modifier.size(16.dp))
                }
            }

            drug.effect?.let {
                if (!"".equals(it)) {
                    Text(
                        text = stringResource(id = R.string.drugEffect),
                        lineHeight = MaterialTheme.typography.titleLarge.lineHeight,
                        fontSize = MaterialTheme.typography.titleLarge.fontSize,
                        fontWeight = MaterialTheme.typography.titleLarge.fontWeight,
                        fontStyle = MaterialTheme.typography.titleLarge.fontStyle
                    )

                    Text(
                        text = it,
                        lineHeight = MaterialTheme.typography.bodyMedium.lineHeight,
                        fontSize = MaterialTheme.typography.bodyMedium.fontSize,
                        fontStyle = MaterialTheme.typography.bodyMedium.fontStyle,
                        fontWeight = MaterialTheme.typography.bodyMedium.fontWeight
                    )

                    Spacer(modifier = Modifier.size(16.dp))
                }
            }

            drug.treat?.let {
                if (!"".equals(it)) {
                    Text(
                        text = stringResource(id = R.string.drugTreat),
                        lineHeight = MaterialTheme.typography.titleLarge.lineHeight,
                        fontSize = MaterialTheme.typography.titleLarge.fontSize,
                        fontWeight = MaterialTheme.typography.titleLarge.fontWeight,
                        fontStyle = MaterialTheme.typography.titleLarge.fontStyle
                    )

                    Text(
                        text = it,
                        lineHeight = MaterialTheme.typography.bodyMedium.lineHeight,
                        fontSize = MaterialTheme.typography.bodyMedium.fontSize,
                        fontStyle = MaterialTheme.typography.bodyMedium.fontStyle,
                        fontWeight = MaterialTheme.typography.bodyMedium.fontWeight
                    )
                }
            }
        }

    }
}